package com.ckzp.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by 51594 on 2024/3/29.
 */
public class DesensitizedUtil {

    // 手机号码正则表达式，匹配前三位和后四位，中间用“****”代替
    private static final Pattern MOBILE_PATTERN = Pattern.compile("1([3-9])(\\d{1})\\d{4}(\\d{4})");

    // 身份证号码正则表达式，匹配前四位和后四位，中间用“**********”代替
    private static final Pattern ID_CARD_PATTERN = Pattern.compile("(\\d{4})\\d{10}(\\w{4})");

    // 姓名正则表达式，匹配姓名的第一个汉字和后面的字符，后面的字符用“*”代替
    private static final Pattern NAME_PATTERN = Pattern.compile("([\\u4e00-\\u9fa5]{1})(.*)");

    // 地址正则表达式，匹配地址中的省、市和区，详细地址用“*”代替
    private static final Pattern ADDRESS_PATTERN = Pattern.compile("([\\w*\\d*\\u4e00-\\u9fa5]{3})(.*)([\\w*\\d*\\-\\_\\u4e00-\\u9fa5]{2})");

    // 邮箱地址正则表达式，匹配邮箱地址中的用户名和域名，用户名的前几个字符用“*”代替
    private static final Pattern EMAIL_PATTERN = Pattern.compile("(\\w+)(\\w{3})@(\\w+\\.\\w+)");

    // 银行卡号正则表达式，匹配前六位和后四位，中间用“****”代替
    private static final Pattern BANK_CARD_PATTERN = Pattern.compile("(\\d{4})(\\d{4})\\d+(\\d{4})(\\d{4})");
    private static final Pattern BANK_CARD_PATTERN_2 = Pattern.compile("(\\d++) (\\d++) (\\d++)");
    private static final Pattern AA = Pattern.compile("path=([a-zA-Z0-9])\\\"");

    /**
     * 手机号脱敏
     *
     * @param value
     * @return
     */
    public static String mobilePhone(String value) {
        Matcher matche = MOBILE_PATTERN.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("1$1$2****$3");
        }
        return value;
    }

    /**
     * 姓名脱敏
     *
     * @param value
     * @return
     */
    public static String name(String value) {
        Matcher matche = NAME_PATTERN.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("$1**");
        }
        return value;
    }

    /**
     * 姓名脱敏
     *
     * @param value
     * @return
     */
    public static String url(String value) {
        Matcher matche = AA.matcher(value);
        while (matche.find()) {
            System.out.println(matche.group(1));
            value = matche.replaceAll("$1");
        }
        return value;
    }

    /**
     * 邮箱
     *
     * @param value
     * @return
     */
    public static String email(String value) {
        Matcher matche = EMAIL_PATTERN.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("$1***@$3");
        }
        return value;
    }

    /**
     * 身份证
     *
     * @param value
     * @return
     */
    public static String idCardNum(String value) {
        Matcher matche = ID_CARD_PATTERN.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("$1**********$2");
        }
        return value;
    }


    /**
     * 银行卡
     *
     * @param value
     * @return
     */
    public static String bankCard(String value) {
        Matcher matche = BANK_CARD_PATTERN.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("$1**********$4");
        }
        matche = BANK_CARD_PATTERN_2.matcher(value);
        while (matche.find()) {
            value = matche.replaceAll("$1 **** $2");
        }
        return value;
    }


    /**
     * 银行卡
     *
     * @param value
     * @return
     */
    public static String address(String value) {
        if (value == null) return "";
        Matcher matche = ADDRESS_PATTERN.matcher(value);
        if (matche.find()) {
            value = matche.replaceAll("$1****$3");
        }
        return value;
    }

    /**
     * @param value
     * @return
     */
    public static String format(String value) {
        value = mobilePhone(value);
        value = email(value);
        value = bankCard(value);
        value = idCardNum(value);
        return value;
    }


    public static void main(String[] arg) {
        String s = "13698861654,对不对,14565434567";
        String s1 = "13698861654";
        String s2 = "wuyang@qq.com，另外也还有一个  111dd@trip.com";
        String s3 = "511324198003108975另外也还有一个  111dd@trip.com";
        String s4 = "6228765676562768123 另外也还有一个 6554 7722 726288811";
        String ss = s + s1 + s2 + s3 + s4;
////        Matcher matche = MOBILE_PATTERN.matcher(s);
////        while (matche.find()) {
////            System.out.println(matche.group());
////            System.out.println(matche.replaceAll("1$1$2****$3"));
////        }
////        //  s.re
//        System.out.println(address("什么什么路23-3-2&3"));
//        System.out.println(format(ss));
//        System.out.println(name("吴阳"));
//        String urls = "11<img src=\"https://tpfub.tyfo.com:4434/tyfoSrvEx/imagedeal?path=aaa\">22";
//        System.out.println(url(urls));
        System.out.println(mobilePhone("这儿有手机号13980705066"));
    }
}
